package spring.springwaiters;

import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import spring.springentity.entity.table.CoffeeEntity;
import sun.net.www.http.HttpClient;

import java.io.IOException;
import java.net.URI;

@Slf4j
@SpringBootApplication
public class SpringWaitersApplication implements CommandLineRunner {

  private HttpClient httpClient;

  public static void main(String[] args) {
    SpringApplication.run(SpringWaitersApplication.class, args);
  }

  @Override
  public void run(String... args) throws Exception {
    // 1.httpClient访问
    httpClientGet();
    // 2.webClient访问 reactive方式订阅 异步，结果返回会稍慢
    webClientMethos();
    // 3.RestTemplate
    getRestTemplate();

  }

  /**
   * RestTemplate
   */
  private static void getRestTemplate() {
    log.info("3.RestTemplate访问");
    RestTemplate rt = new RestTemplate();
    log.info("RestTemplate GET访问返回:" + rt.getForObject("http://localhost:8192/TestRest/json/MOCHA", String.class));
    CoffeeEntity coffee = new CoffeeEntity();
    coffee.setName("MACCHIATO");
    log.info("RestTemplate POST访问返回:" + rt.postForObject("http://localhost:8192/TestRest/postjson", coffee, CoffeeEntity.class));
  }

  /**
   * httpClient访问
   */
  private void httpClientGet() {
    log.info("1.httpClient访问");

    CloseableHttpClient httpClient = null;
    // 响应模型
    CloseableHttpResponse response = null;
    // 响应模型
    CloseableHttpResponse responsePost = null;

    try {
      // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
      httpClient = HttpClientBuilder.create().build();
      // 1.Get请求
      HttpGet httpGet = new HttpGet("http://localhost:8192/TestRest/json/ESPRESSO");
      // 由客户端执行(发送)Get请求
      response = httpClient.execute(httpGet);
      // 从响应模型中获取响应实体
      HttpEntity responseEntity = response.getEntity();
      log.info("httpClient Get访问");
      log.info("响应状态为:" + response.getStatusLine());
      if (responseEntity != null) {
        log.info("响应内容长度为:" + responseEntity.getContentLength());
        log.info("响应内容为:" + EntityUtils.toString(responseEntity));
      }

      //// 2.Post请求
      JSONObject json = new JSONObject();
      json.set("name", "CAPUCCINO");
      URI uri = RequestBuilder.post("http://localhost:8192/TestRest/postjson").build().getURI();
      HttpPost post = new HttpPost(uri);
      HttpEntity httpEntity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
      post.setEntity(httpEntity);
      responsePost = httpClient.execute(post);
      // 从响应模型中获取响应实体
      HttpEntity responsePostEntity = responsePost.getEntity();

      log.info("httpClient Post访问");
      log.info("响应状态为:" + responsePost.getStatusLine());
      if (responsePostEntity != null) {
        log.info("响应内容长度为:" + responsePostEntity.getContentLength());
        log.info("响应内容为:" + EntityUtils.toString(responsePostEntity));
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        // 释放资源
        if (httpClient != null) {
          httpClient.close();
        }
        if (response != null) {
          response.close();
        }
        if (responsePost != null) {
          responsePost.close();
        }

      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  private void webClientMethos() {
    log.info("2.webClient访问");
    WebClient webClient = WebClient.create();
    Mono<String> mono = webClient.get().uri("http://localhost:8192/TestRest/xml/LATTE").retrieve().bodyToMono(String.class);
    mono.subscribe(data -> {
      log.info("webClient Get访问返回:" + data);
    });
    // 2.Post请求
    CoffeeEntity coffee = new CoffeeEntity();
    coffee.setName("CAPUCCINO");
    Mono<String> monoPost =
      webClient.post().uri("http://localhost:8192/TestRest/postxml").bodyValue(coffee).retrieve().bodyToMono(String.class);
    monoPost.subscribe(data -> {
      log.info("webClient Post访问返回:" + data);
    });

  }

}
